$(function ()
{

    //设置指定面板展开
    $('#layout').layout('expand', 'north');

    //设置开始时间
    setStartTime();

    var datagrid;
    datagrid = $("#dg").datagrid(
        {
           url : "../../api/dd_findAllOrder",
            //isField : "testnum",
            method : "get",
            width : 1000,
            height : 550,
            rownumbers : true, //显示行号
            singleSelect : true,//只支持单选
            toolbar : '#tc',//设置工具栏的id
            pagination : true, //显示分页
            pageSize : 10, //分页大小
            pageList : [5, 10, 15, 20], //每页的个数
            beforePageText : '第',
            afterPageText : '页    共 {pages} 页',
            displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            fit : true, //自动补全
            fitColumns : true,
            title : "",
            columns : [[//每个列具体内容
                    {
                        field : 'ck',
                        checkbox : true
                    },
                     {
                        field : 'orderNo',
                        title : '订单编码',
                        width : 100,
                        align : 'center'
                       
                    },
                    {
                        field : 'productLine',
                        title : '线体名称',
                        width : 150,
                        align : 'center'
                        
                    },
                    {
                        field : 'productLineNo',
                        title : '线体编码',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'productDescription',
                        title : '型号描述',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'planCount',
                        title : '计划数量',
                        width : 100,
                        align : 'center'
                    },
                     {
                        field : 'checkCount',
                        title : '应抽数量',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'checkDate',
                        title : '计划时间',
                        width : 100,
                        align : 'center',
                        formatter:function(val,row){
                            var now = new Date(val);
                            return now.format("yyyy-MM-dd hh:mm:ss");
                        }
                    },
                    {
                        field : 'checkState',
                        title : '抽样检测状态',
                        width : 130,
                        align : 'center',
                        formatter:function(value , record , index)
                        {
                            if(value==0){
                                return '<span style=color:red; >待检测</span>' ;
                            }else if( value ==1){
                                return '<span style=color:green; >检测中</span>' ; 
                            }else if( value ==2){
                                return '<span style=color:green; >整改中</span>' ; 
                            }else if( value ==3){
                                return '<span style=color:green; >检测完成</span>' ; 
                            }else if( value ==4){
                                return '<span style=color:green; >报告审核</span>' ; 
                            }else if(value ==5){
                                return '<span style=color:green; >报告审批</span>' ; 
                            }
                            else{
                                return '<span style=color:green; >订单结束</span>' ; 
                            }
                        }
                    },
                ]],
            toolbar : [//工具条
                {
                    text : "查看订单",
                    handler : function ()
                    {

                        var rows = datagrid.datagrid('getSelections');

                        if (rows.length <= 0)
                        {
                            $.messager.alert('警告', '您没有选择', 'error');
                        }
                        else if (rows.length > 1)
                        {
                            $.messager.alert('警告', '不支持批量接收', 'error');
                        }
                        else
                        {

                            var index = datagrid.datagrid('getRowIndex', datagrid.datagrid('getSelected'));
                            var row1 = datagrid.datagrid('getData').rows[index];

                            // window.location.href="../../menu/OrderManage/btn_ViewOrderOfReportRelease?testnum="+row1.testnum;

                            window.open("../../menu/OrderManage/btn_ViewOrderOfReportRelease?testnum=" + row1.testnum);
                        }
                    }
                },

                {
                    text : "进度查询",
                    handler : function ()
                    {

                        var rows = datagrid.datagrid('getSelections');

                        if (rows.length <= 0)
                        {
                            $.messager.alert('警告', '您没有选择', 'error');
                        }
                        else if (rows.length > 1)
                        {
                            $.messager.alert('警告', '不支持批量进度查询', 'error');
                        }
                        else
                        {

                            var index = datagrid.datagrid('getRowIndex', datagrid.datagrid('getSelected'));
                            var row1 = datagrid.datagrid('getData').rows[index];

                            // window.location.href="../../menu/OrderManage/btn_ViewOrderOfReportRelease?testnum="+row1.testnum;
                            // alert(row1.testnum);
                            window.open("../../menu/ReportManage/OrderProgress?testnum=" + row1.testnum);
                        }
                    }
                }
            ],
            onAfterEdit : function (rowIndex, rowData, changes)
            {
                var inserted = datagrid.datagrid('getChanges', 'inserted');
                var updated = datagrid.datagrid('getChanges', 'updated');
                if (inserted.length < 1 && updated.length < 1)
                {
                    editRow = undefined;
                    datagrid.datagrid('unselectAll');
                    return;
                }

                var url = '';
                $.messager.alert("信息", inserted.length, "info");
                if (inserted.length > 0)
                {
                    url = 'insertsampleinfo.do';
                }

                if (updated.length > 0)
                {
                    url = 'updateSampleInfo.do';
                }
                $.ajax(
                {
                    url : url,
                    data : rowData,
                    dataType : 'json',
                    success : function (r)
                    {
                        if (r.success)
                        {
                            datagrid.datagrid('acceptChanges');
                            $.messager.show(
                            {
                                msg : r.msg,
                                title : '成功'
                            }
                            );
                            editRow = undefined;
                            datagrid.datagrid('reload');
                        }
                        else
                        {
                            /*datagrid.datagrid('rejectChanges');*/
                            datagrid.datagrid('beginEdit', editRow);
                            $.messager.alert('错误', r.msg, 'error');
                        }
                        datagrid.datagrid('unselectAll');
                    }
                }
                );

            },
            onSelect : function (index, row)
            {
                console.log(index);
                console.log(row.status);
                $("#l0").html(row.testnum);
                setProcess(row);
            },
        }
        );

}
);

//整体模糊查询
function searchTemporarySam()
{

    var orderno = $('#orderno').textbox('getValue');
    var samplename = $('#samplename').combobox('getText');
    var sampletype = $('#sampletype').searchbox('getValue');
    var producttype = $('#producttype').combobox('getText');
    var bailor = $('#bailor').textbox('getValue');
    // var bailor="";
    var samplegrade = $('#samplegrade').combobox('getText');
    var purpose = $('#purpose').combobox('getText');

    var starttime = $('#starttime').datetimebox('getValue');
    var endtime = $('#endtime').datetimebox('getValue');

    var usernumber = userno + "@" + teammemyau;
    var departmentnumber = departmentid + "@" + depidbyau;

    var modelinnermachine = $('#modelinnermachine').textbox('getValue');
    var modeloutermachine = $('#modeloutermachine').textbox('getValue');

    //不同角色查询的订单信息不一样
    var url1 = "";
    if (authorityString.indexOf("锁定委托人") >= 0)
    {
        url1 = '../../api/searchAllOrderInfoBySomeKey?testnum=' + orderno + '&samplename=' + samplename + '&sampletype=' + sampletype + '&producttype=' + producttype + '&bailor=' + bailor + '&samplegrade=' + samplegrade + '&purpose=' + purpose + '&realname=' + realname + '&departmentname=' + departmentname + '&productcategoryid=' + producttype + '&bailorno=' + usernumber + "&starttime=" + starttime + "&endtime=" + endtime + '&modelinnermachine=' + modelinnermachine + '&modeloutermachine=' + modeloutermachine;
    }
    else
    {
        url1 = '../../api/searchAllOrderInfoBySomeKey?testnum=' + orderno + '&samplename=' + samplename + '&sampletype=' + sampletype + '&producttype=' + producttype + '&bailor=' + bailor + '&samplegrade=' + samplegrade + '&purpose=' + purpose + '&realname=' + realname + '&departmentname=' + departmentname + '&productcategoryid=' + producttype + "&starttime=" + starttime + "&endtime=" + endtime + '&modelinnermachine=' + modelinnermachine + '&modeloutermachine=' + modeloutermachine;
    }
    //reload形式加载模糊查询,成功
    $('#dg').datagrid('reload', url1);

}


